<?php
header("Content-Type:text/html; charset=utf-8");
include_once('inc/init.php');
include_once('inc/cache.php');
require_once getCacheFilePath('forums.php');
$action=strFilter($_GET["action"]);
$fid=numFilter($_POST['fid']);
if(!isForumAdmin($fid)){
	exit("您没有权限管理该版块。");
}
$topic_row=null;
$tid=numFilter($_POST['tid']);
$reason=strFilter($_POST['reason']);
$postid=numFilter($_POST['postid']);

if(!empty($tid)){
	$topic_row=$db->row_select_one("topics","id={$tid} and fid={$fid}");
	if(empty($topic_row)){
		exit("帖子不存在。");
	}
}

switch($action){
	case "delPost":
		$tid=numFilter($_POST['tid']);
		if(empty($postid)){
			echo("没有选择帖子。");
		}
		else{
			if($tid==0){
				//直接删除回帖
				$db->row_delete("posts","id={$postid} and fid={$fid}");
				$attrows=$db->row_select("attachments","postid={$postid}");
				foreach($attrows as $row){
					$filepath="uploadfile/attachment/".$row['filepath'];
					if(file_exists($filepath)){
						@unlink($filepath);
					}
					$db->row_delete("attachments","id={$row['id']}");
				}

			}else{				
				//如果是主题，就先进入回收站
				$topic['isrecycle']=1;
				$db->row_update("topics",$topic,"id={$tid} and fid={$fid}");				
			}
			if($cache_settings['isadminlog']=='1'){
				saveLog('delPost', $lg['username'], $lg['username'], $tid, $postid, '', '', $reason);
			}
			echo("y");
		}
	break;
	
	case "movePost":
		//$fromfid=numFilter($_GET['fromfid']);
		$tofid=numFilter($_POST['selectv']);
		$tid=numFilter($_POST['tid']);
		if(empty($tid)){
			echo("没有选择帖子。");
		}
		else{
			$topic['fid']=$tofid;
			$db->row_update("topics",$topic,"id={$tid} and fid={$fid}");

			$post['fid']=$tofid;
			$db->row_update("posts",$post,"tid={$tid} and fid={$fid}");
			if($cache_settings['isadminlog']=='1'){
				saveLog('movePost', $lg['username'], $lg['username'], $tid, $postid, $tofid, '', $reason);
			}
			echo("y");
		}	
	break;	
	
	case "coverPost":
		$v=numFilter($_POST['selectv']);
		$v=($v==1?1:0);
		$postid=numFilter($_POST['postid']);
		$reason=strFilter($_POST['reason']);
		if(empty($postid)){
			echo("没有选择帖子。");
		}
		else{
			$post['iscover']=$v;
			$db->row_update("posts",$post,"id={$postid} and fid={$fid}");
			if($cache_settings['isadminlog']=='1'){
				saveLog('coverPost', $lg['username'], $lg['username'], $tid, $postid, '', '', $reason);
			}
			echo("y");
		}
	break;
	
	case "lockPost":
		$v=numFilter($_POST['selectv']);
		$v=($v==1?1:0);
		$tid=numFilter($_POST['tid']);
		$reason=strFilter($_POST['reason']);
		if(empty($tid)){
			echo("没有选择帖子。");
		}
		else{
			$topic['islock']=$v;
			$db->row_update("topics",$topic,"id={$tid} and fid={$fid}");
			if($cache_settings['isadminlog']=='1'){
				saveLog('lockPost', $lg['username'], $lg['username'], $tid, $postid, '', '', $reason);
			}
			echo("y");
		}
	break;

	case "topPost":
		$v=numFilter($_POST['selectv']);
		$v=($v==1?1:0);
		$tid=numFilter($_POST['tid']);
		$reason=strFilter($_POST['reason']);
		if(empty($tid)){
			echo("没有选择帖子。");
		}
		else{
			$topic['istop']=$v;
			$db->row_update("topics",$topic,"id={$tid} and fid={$fid}");
			if($cache_settings['isadminlog']=='1'){
				saveLog('topPost', $lg['username'], $lg['username'], $tid, $postid, '', '', $reason);
			}
			echo("y");
		}
	break;

	case "alltopPost":
		$v=numFilter($_POST['selectv']);
		$v=($v==1?1:0);
		$tid=numFilter($_POST['tid']);
		if(empty($tid)){
			echo("没有选择帖子。");
		}
		else{
			$topic['isalltop']=$v;
			$db->row_update("topics",$topic,"id={$tid} and fid={$fid}");
			if($cache_settings['isadminlog']=='1'){
				saveLog('alltopPost', $lg['username'], $lg['username'], $tid, $postid, '', '', $reason);
			}
			echo("y");
		}
	break;

	case "goodPost":
		$v=numFilter($_POST['selectv']);
		$v=($v==1?1:0);
		if(empty($tid)){
			exit("没有选择帖子。");
		}
		
		$topic['isgood']=$v;
		$db->row_update("topics",$topic,"id={$tid}");
		updateCredits($topic_row['userid'], 'goodvar');
		if($cache_settings['isadminlog']=='1'){
			saveLog('goodPost', $lg['username'], $lg['username'], $tid, $postid, '', '', $reason);
		}
		echo("y");
				
	break;

	case "upPost":
		$tid=numFilter($_POST['tid']);
		if(empty($tid)){
			echo("没有选择帖子。");
		}
		else{
			$topic['ordertime']=time();
			$db->row_update("topics",$topic,"id={$tid} and fid={$fid}");
			if($cache_settings['isadminlog']=='1'){
				saveLog('upPost', $lg['username'], $lg['username'], $tid, $postid, '', '', $reason);
			}
			echo("y");
		}
	break;

	case "stampPost":
		$v=numFilter($_POST['selectv']);
		$tid=numFilter($_POST['tid']);
		if(empty($tid)){
			echo("没有选择帖子。");
		}
		else{
			$topic['stamp']=$v;
			$db->row_update("topics",$topic,"id={$tid} and fid={$fid}");
			if($cache_settings['isadminlog']=='1'){
				saveLog('stampPost', $lg['username'], $lg['username'], $tid, $postid, '', '', $reason);
			}
			echo("y");
		}		
	break;

	case "highlightPost":
		$tid=numFilter($_POST['tid']);
		$color=$_POST['highlightfontC'];
		$isb=intval($_POST['highlightfontB']);
		$isi=intval($_POST['highlightfontI']);
		$isu=intval($_POST['highlightfontU']);
		$hlstr='';
		if(!empty($color) || $isb || $isi || $isu){
			$hlstr="{$color},{$isb},{$isi},{$isu}";
		}
		if(empty($tid)){
			echo("没有选择帖子。");
		}
		else{
			$topic['highlight']=$hlstr;
			$db->row_update("topics",$topic,"id={$tid} and fid={$fid}");
			if($cache_settings['isadminlog']=='1'){
				saveLog('highlightPost', $lg['username'], $lg['username'], $tid, $postid, '', '', $reason);
			}
			echo("y");
		}
	break;

/*
	case "saveforumdetails":
		try{
			$forum['rules']=strFilter($_POST['rules']);
			$fid=numFilter($_GET['fid']);
			$db->row_update("forums",$forum,"id={$fid}");
			cacheForum();
			echo("y");
		}catch(Exception $e){
			echo($e);
		}
	break;
*/

	case "dotopics":
		try{
			$postaction=$_POST['postaction'];
			$tids=$_POST['tids'];
			if(empty($tids)){
				echo("没有选中帖子。");
				return;
			}
			if(is_array($tids)) {
				$tidstr=implode(",",$tids);
				switch($postaction){
					
					case "delPost":
						$db->row_delete("posts","tid in ({$tidstr}) and fid={$fid}");
						$db->row_delete("topics","id in ({$tidstr}) and fid={$fid}");
						echo("y");
					break;
					
					case "movePost":
						$movetofid=numFilter($_POST['movetofid']);
						$post['fid']=$movetofid;
						$db->row_update("posts",$post,"tid in ({$tidstr}) and fid={$fid}");
						$topic['fid']=$movetofid;
						$db->row_update("topics",$topic,"id in ({$tidstr}) and fid={$fid}");
						echo("y");
					break;	
					
					case "coverPost":
					case "coverPost_":
						$v=($postaction=='coverPost'?1:0);
						$post['iscover']=$v;
						$db->row_update("posts",$post,"tid in ({$tidstr}) and istopic=1 and fid={$fid}");
						echo("y");
					break;
					
					case "lockPost":
					case "lockPost_":
						$v=($postaction=='lockPost'?1:0);
						$topic['islock']=$v;
						$db->row_update("topics",$topic,"id in ({$tidstr}) and fid={$fid}");
						echo("y");
					break;

					case "topPost":
					case "topPost_":
						$v=($postaction=='topPost'?1:0);
						$topic['istop']=$v;
						$db->row_update("topics",$topic,"id in ({$tidstr}) and fid={$fid}");
						echo("y");
					break;

					case "alltopPost":
					case "alltopPost_":
						$v=($postaction=='alltopPost'?1:0);
						$topic['isalltop']=$v;
						$db->row_update("topics",$topic,"id in ({$tidstr}) and fid={$fid}");
						echo("y");
					break;

					case "goodPost":
					case "goodPost_":
						$v=($postaction=='goodPost'?1:0);
						$topic['isgood']=$v;
						$db->row_update("topics",$topic,"id in ({$tidstr}) and fid={$fid}");
						echo("y");
					break;

					case "upPost":
						$topic['ordertime']=time();
						$db->row_update("topics",$topic,"id in ({$tidstr}) and fid={$fid}");
						echo("y");
					break;

					case "stampPost":
						$v=numFilter($_POST['stampid']);
						$topic['stamp']=$v;
						$db->row_update("topics",$topic,"id in ({$tidstr}) and fid={$fid}");
						echo("y");
					break;
					
					case "highlightPost":
						$color=$_POST['highlightfontC'];
						$isb=intval($_POST['highlightfontB']);
						$isi=intval($_POST['highlightfontI']);
						$isu=intval($_POST['highlightfontU']);
						$hlstr='';
						if(!empty($color) || $isb || $isi || $isu){
							$hlstr="{$color},{$isb},{$isi},{$isu}";
						}
						$topic['highlight']=$hlstr;
						$db->row_update("topics",$topic,"id in ({$tidstr}) and fid={$fid}");
						echo("y");
					break;

					case "restorePost":
						$topic['isrecycle']=0;
						$db->row_update("topics",$topic,"id in ({$tidstr}) and fid={$fid}");
						echo("y");
					break;

					case "delPost":
						$db->row_delete("posts","tid in ({$tidstr}) and fid={$fid}");
						$db->row_delete("topics","id in ({$tidstr}) and fid={$fid}");
						echo("y");
					break;
					
					case "restorePost":
						$topic['isrecycle']=0;
						$db->row_update("topics",$topic,"id in ({$tidstr}) and fid={$fid}");
						echo("y");
					break;


					default:
						echo"No Such Action";
					break;
				}
			}
		}catch(Exception $e){
			echo($e);
		}
	break;

	case "verifytopic":
		try{
			$postaction=numFilter($_POST['postaction']);
			$tids=$_POST['tids'];
			if(empty($tids)){
				echo("没有选中帖子。");
				return;
			}
			if(is_array($tids)) {
				$tidstr=implode(",",$tids);
				if($postaction==1){
					//审核通过
					$topic['verifystate'] = 0;
					$db->row_update("topics",$topic,"id in ({$tidstr}) and fid={$fid}");
					$post['verifystate'] = 0;
					$db->row_update("posts",$post,"tid in ({$tidstr}) and istopic=1 and fid={$fid}");
				}elseif($postaction==2){
					//删除
					$db->row_delete("posts","tid in ({$tidstr}) and fid={$fid}");
					$db->row_delete("topics","id in ({$tidstr}) and fid={$fid}");
				}else{
					//忽略
				}
			}
			echo("y");
		}catch(Exception $e){
			echo($e);
		}
	break;


	case "verifypost":
		try{
			$postaction=numFilter($_POST['postaction']);
			$ids=$_POST['ids'];
			if(empty($ids)){
				echo("没有选中帖子。");
				return;
			}
			if(is_array($ids)) {
				$idstr=implode(",",$ids);
				if($postaction==1){
					//审核通过
					$post['verifystate'] = 0;
					$db->row_update("posts",$post,"id in ({$idstr}) and fid={$fid}");
				}elseif($postaction==2){
					//删除
					$db->row_delete("posts","id in ({$idstr}) and fid={$fid}");
				}else{
					//忽略
				}
			}
			echo("y");
		}catch(Exception $e){
			echo($e);
		}
	break;

	case "postannounces":
		try{
			$anc=$_POST['anc'];
			$anc['starttime'] = empty($anc['starttime'])?0:strtotime($anc['starttime'])-$cache_settings['timeoffset']*3600;
			$anc['stoptime'] = empty($anc['stoptime'])?0:strtotime($anc['stoptime'])+24*3600-$cache_settings['timeoffset']*3600-1;
			$anc['posttime'] = time();
			$ancid=numFilter($_POST['ancid']);
			$anc['username'] = $lg['username'];
			$anc['userid'] = $lg['userid'];
			$anc['fid'] = $fid;
			$anc['targets'] = $fid;
			if(numFilter($anc['type'])==1){
				$anc['content']='';
			}
			if(empty($ancid)){
				$db->row_insert("announces",$anc);
			}else{
				$db->row_update("announces",$anc,"id={$ancid} and fid={$fid}");
			}
			writeAnnouncesCache();
			echo("y");

		}catch(Exception $e){
			echo($e);
		}
	break;

	case "clean":
		$db->row_delete("posts","tid in (select id from `{$db->pre}topics` where isrecycle=1 and fid={$fid})");
		$db->row_delete("topics","isrecycle=1 and fid={$fid}");
		echo("y");
	break;

	case "doannounces":
		$ids=$_POST['ids'];
		$ordernum=$_POST['ordernum'];

		if(!empty($ids) && is_array($ids)){
			$idstr = implode(",", $ids);
			$db->row_delete("announces","id in ($idstr) and fid={$fid}");
		}

		if(is_array($ordernum)){
			foreach($ordernum as $key=>$order){
				$anc['ordernum']=$order;
				$db->row_update("announces",$anc,"id={$key} and fid={$fid}");
			}
		}
		writeAnnouncesCache();
		echo("y");
	break;

	

	default:
		echo"No Such Action";
	break;
}


function saveLog($type,$username1,$username2,$field1,$field2,$field3,$field4,$reason){
	global $db;
	$adminlog['type']= $type;
	$adminlog['username1']= $username1;
	$adminlog['username2']= $username2;
	$adminlog['field1']= $field1;
	$adminlog['field2']= $field2;
	$adminlog['field3']= $field3;
	$adminlog['field4']= $field4;
	$adminlog['reason']= $reason;
	$adminlog['createtime'] = time();
	$adminlog['ip'] = getIP();
	$db->row_insert("adminlog",$adminlog);
}

?>
